package com.atguigu.tingshu.order.context;

import com.atguigu.tingshu.order.strategy.SaveOrderStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
@Slf4j
public class SaveOrderStrategyContext implements ApplicationContextAware {
    private static final Map<String, SaveOrderStrategy> MAP = new ConcurrentHashMap<>();

    public static SaveOrderStrategy getSaveOrderStrategy(String payWay){
        return MAP.get(payWay);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, SaveOrderStrategy> beans = applicationContext.getBeansOfType(SaveOrderStrategy.class);
        beans.values().stream().forEach(saveOrderStrategy->MAP.put(saveOrderStrategy.getPayWay(),saveOrderStrategy));
    }
}
